
using Youz.Common.Entity;
using Youz.Common.Helper;
using Youz.CreditRepay.DAL;
using Youz.CreditRepay.Entity;
using System;
using System.Data;
using System.Data.SqlClient;

namespace Youz.CreditRepay.BLL
{
	/// <summary>
	/// Member_Recharge BLL Class
	/// </summary>
	public partial class Member_RechargeBLL : BaseBLL
	{
		Member_RechargeDAL dal;
		
		public Member_RechargeBLL()
		{
			dal = new Member_RechargeDAL();
			base.ModuleName = "Member_Recharge";
			base.MenuName = "Member_Recharge";
		}

		#region  成员方法
		/// <summary>
		///  新增一条数据
		/// </summary>
		public bool Add(Member_Recharge model)
		{
			var r = dal.Add(model);
			
			base.WriteOperationLog(OperationType.Add, r, Core.GetJsonByObjectHaveTime(model));

			return r;
		}

		/// <summary>
		///  保存一条数据
		/// </summary>
		public bool Update(Member_Recharge model)
		{
			var r = dal.Update(model);
			
			base.WriteOperationLog(OperationType.Update, r, Core.GetJsonByObjectHaveTime(model));

			return r;
		}

		/// <summary>
		/// 删除一条数据
		///</summary>
		public bool Delete(String RechargeID)
		{
			var r = dal.Delete(RechargeID);
			
			base.WriteOperationLog(OperationType.Delete, r, RechargeID);

			return r;
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Member_Recharge Get(String RechargeID)
		{
			return dal.Get(RechargeID);
		}
		
		/// <summary>
		/// 得到一个JSON对象
		/// </summary>
		public string GetJson(String RechargeID)
		{
            return Core.GetJsonByObjectHaveTime(Get(RechargeID));
		}

        public string GetRateJson(String RechargeID)
        {
            return Core.GetJsonByObjectHaveTime(dal.GetRate(RechargeID));
        }

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet Gets(string sqlWhere)
		{
			return dal.Gets(sqlWhere);
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet Gets(int top, string sqlWhere, string filedOrder)
		{
			return dal.Gets(top, sqlWhere, filedOrder);
		}
		
		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string sqlWhere)
		{
			return dal.GetRecordCount(sqlWhere);
		}
		
		/// <summary>
		/// 获得数据列表分页
		/// </summary>
		public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
		{
			return dal.Gets(sqlWhere, pageindex, pagesize, out total, cmdParms);
		}
		#endregion  成员方法

        public string Validator(Member_Recharge model)
        {
            if (model.FromAmount < 0)
                return "充值金额不能小于0";
            if (string.IsNullOrWhiteSpace(model.FromBankName))
                return "请选择您的银行账户信息";
            if (string.IsNullOrWhiteSpace(model.ToBankName))
                return "请选择公司银行账户信息";
            if (string.IsNullOrWhiteSpace(model.FromBankAccountNo))
                return "请选择您的银行账户信息";
            if (string.IsNullOrWhiteSpace(model.ToBankAccountNo))
                return "请选择公司银行账户信息";
            if (string.IsNullOrWhiteSpace(model.FromBankAccountName))
                return "请选择您的银行账户信息";
            if (string.IsNullOrWhiteSpace(model.ToBankAccountName))
                return "请选择公司银行账户信息";
            if (model.TransferTime.ToString() == "0001/1/1 0:00:00")
                return "请选择汇款日期";
            return string.Empty;
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="RechargeID">验证单号</param>
        /// <param name="Status">状态</param>
        /// <returns></returns>
        public bool UpdateRechargeStatus(string RechargeID, RechargeStatus Status)
        {
            bool result = dal.UpdateRechargeStatus(RechargeID, Status);
            if (Status == RechargeStatus.Audit)
            {
                WriteOperationLogSensitive(OperationType.Update, result, RechargeID, 1, "账户充值");
            }
            else
            {
                WriteOperationLog(OperationType.Update, result, RechargeID);
            }
            
            return result;
        }

        /// <summary>
        /// 保存审核
        /// </summary>
        /// <param name="model"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveAudit(RechargeStatus status, decimal toAmount, string auditOpinion, Member_Recharge model)
        {
            try
            {
                #region 状态变更表
                Sys_StatusRecord RecordModel = new Sys_StatusRecord();
                RecordModel.RecordId = Guid.NewGuid();
                RecordModel.MemberAccount = model.MemberAccount;
                RecordModel.RecordTable = Youz.Common.Entity.Sys_StatusRecord.StatusRecordType.Member_Recharge.ToString();
                RecordModel.RecordType = "Status";
                RecordModel.BeforeValue = (int)model.Status;
                RecordModel.AfterValue = (int)status;
                RecordModel.CreateTime = DateTime.Now;
                RecordModel.Creator = model.MemberAccount;
                RecordModel.Memo = auditOpinion;
                RecordModel.ObjectID = model.RechargeID.ToString();
                RecordModel.LoginID = Core.MemberLoginID;
                #endregion

                #region 充值表
                model.Status = status;
                model.ToAmount = toAmount;
                model.ModifyTime = DateTime.Now;
                #endregion

                bool result = dal.SaveAudit(model, RecordModel);
                WriteOperationLog(OperationType.Update, result, Core.GetJsonByObjectHaveTime(model));
                return result;
            }
            catch (Exception ex)
            {
                Core.WriteErrorLog(ex);
                return false;
            }
        }

        /// <summary>
        /// 按日期统计充值金额和数量
        /// </summary>
        /// <returns></returns>
        public string GetRechargeChart()
        {
            DataTable dt = dal.GetRechargeChart();
            return string.Format("[{0},{1}]", TypeHelper.ConvertToChart(dt, 1), TypeHelper.ConvertToChart(dt, 2));
        }

        public Int64 GenerateID()
        {
            return dal.GenerateID();
        }
	}
}


  